package com.cgsrp.cgsrp_baby.controller;

import com.cgsrp.cgsrp_baby.domain.ChildParent;
import com.cgsrp.cgsrp_baby.domain.ChildParentRelation;
import com.cgsrp.cgsrp_baby.domain.Message;
import com.cgsrp.cgsrp_baby.service.ChildParentRelationService;
import com.cgsrp.cgsrp_baby.service.MessageService;
import com.cgsrp.utils.Result;
import com.github.pagehelper.PageInfo;
// import org.apache.poi.poifs.filesystem.DirectoryEntry;
// import org.apache.poi.poifs.filesystem.DocumentEntry;
// import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.util.List;

@RestController
@RequestMapping("message")
public class MessageController {
    @Autowired
    private ChildParentRelationService relationService;
    @Autowired
    private MessageService messageService;

    @GetMapping("list")
    public List<Message> list(int userId) {
        return messageService.list(userId);
    }

    @PostMapping("del")
    public Result del(Integer id){
        return messageService.del(id);
    }




//     富文本下载
/*     @ResponseBody
    @RequestMapping(value = "download")
    public int download(HttpServletResponse response, HttpServletRequest request) throws Exception {
        String content = "<h1>标题头</h1><h2>第二个标题</h2><a href=\"www.baidu.com\">百度搜索</a>";
        StringBuffer sbf = new StringBuffer();
        sbf.append("<html><body>");
        sbf.append(content);
        sbf.append("</body></html");
        exportWord(request, response, String.valueOf(sbf), "word1");
        return 1;
    }


     */
    /**
     * @param request
     * @param response
     * @param content  富文本内容
     * @param fileName 生成word名字
     * @throws Exception
     */
    /*
    public static void exportWord(HttpServletRequest request, HttpServletResponse response, String content, String fileName) throws Exception {
        byte b[] = content.getBytes("GBK"); //这里是必须要设置编码的，不然导出中文就会乱码。
        ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
        POIFSFileSystem poifs = new POIFSFileSystem();
        DirectoryEntry directory = poifs.getRoot();
        DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); //该步骤不可省略，否则会出现乱码。
        //输出文件
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/msword");//导出word格式
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GB2312"), "iso8859-1") + ".doc");
        ServletOutputStream ostream = response.getOutputStream();
        poifs.writeFilesystem(ostream);
        bais.close();
        ostream.close();
        poifs.close();
    } */
}
